<script>
	import utils from '@/common/utils.js'
	
	export default {
		globalData: {
			hasLogin: false,
			userInfo: {},
			wx_openid: '',
			uid: ''
		},
		onLaunch: function() {
			uni.showLoading({
				title: "加载数据中",
				mask: true
			})
			this.checkToken()
			
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			// Token校验
			checkToken: function() {
				var that = this;
				console.warn('注意1.1.0版本会返回userInfo，请不要返回全部信息给客户端')
				return new Promise((resolve, reject) => {
					uniCloud.callFunction({
						name: 'user-center',
						data: {
							action: 'checkToken'
						},
						success(res) {
							const {
								code,
								token,
								tokenExpired,
								userInfo,
								uid
							} = res.result
							if (code == 0) {
								that.globalData.hasLogin = true;
								that.globalData.userInfo = userInfo;
								that.globalData.uid = uid;
								
							} else if (code == 30204) {
								// uni.showModal({
								// 	title: "请先登陆",
								// 	success: (res) => {
								// 		if (res.confirm) {
								// 			uni.switchTab({
								// 				url:"/pages/profile/profile"
								// 			})
								// 		}
								// 	}
								// })
							} else if (code == 10001) {
								uni.showModal({
									title: "账号被禁用"
								})
							}
							if(token){
								uni.setStorageSync('uni_id_token', token)
								uni.setStorageSync('uni_id_token_expired', tokenExpired)
								
							}
							resolve(res)
							uni.hideLoading()
						},
						fail(e) {
							console.error(e)
							uni.showModal({
								showCancel: false,
								content: '请求云函数失败，请稍后再试'
							})
							reject(e)
						}
					})
				})
				
			}
		}
		
	}
</script>

<style>
	/*每个页面公共css */
	@import "colorui/main.css";
	@import "colorui/icon.css";
	
	@font-face {
	  font-family: 'iconfont';  /* Project id 2298242 */
	  src: url('//at.alicdn.com/t/font_2298242_sinwhh878rj.woff2?t=1620715879363') format('woff2'),
	       url('//at.alicdn.com/t/font_2298242_sinwhh878rj.woff?t=1620715879363') format('woff'),
	       url('//at.alicdn.com/t/font_2298242_sinwhh878rj.ttf?t=1620715879363') format('truetype');
	}
	
	.iconfont{
	    font-family:"iconfont" !important;
	    font-style:normal;
	    -webkit-font-smoothing: antialiased;
	    -webkit-text-stroke-width: 0.2px;
	    -moz-osx-font-smoothing: grayscale;}
		
	.main-btn {
		background-color: #F0A93F;
		color: #FFFFFF;
	}
	.sub-btn {
		background-color: #E5E5E5;
		color: #000000;
	}
</style>
